package com.heima.behavior.service.impl;

import com.alibaba.fastjson.JSON;
import com.heima.constants.message.HotArticleConstants;
import com.heima.model.mess.app.NewBehaviorDTO.BehaviorType;

import com.heima.behavior.service.ApLikesBehaviorService;
import com.heima.common.exception.CustException;
import com.heima.model.behavior.dtos.LikesBehaviorDTO;
import com.heima.model.behavior.pojos.ApLikesBehavior;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.mess.app.NewBehaviorDTO;
import com.mongodb.client.result.DeleteResult;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ApLikesBehaviorServiceImpl implements ApLikesBehaviorService {
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    //点赞或取消点赞
    @Override
    public ResponseResult like(LikesBehaviorDTO dto) {
        //判断操作类型
        if (dto.getOperation() == (short) 0) {
            //操作类型为点赞,根据行为实体id查询是否已经点赞
            Query query = Query.query(Criteria.where("entryId").is(dto.getEquipmentId().toString()).and("articleId").is(dto.getArticleId()));
            ApLikesBehavior apLikesBehavior = mongoTemplate.findOne(query, ApLikesBehavior.class);
            //非空判断
            if (apLikesBehavior != null && apLikesBehavior.getOperation() == 0) {
                //点过赞
                CustException.cust(AppHttpCodeEnum.DATA_NOT_ALLOW, "您已经点过赞了!");
            } else if (apLikesBehavior != null && apLikesBehavior.getOperation() == 1) {
                //取消点赞,但未删除数据,删除旧的数据
                DeleteResult remove = mongoTemplate.remove(query, ApLikesBehavior.class);
            }
            //没有点赞,新增数据
            apLikesBehavior = new ApLikesBehavior();
            apLikesBehavior.setType(dto.getType());
            apLikesBehavior.setEntryId(dto.getEquipmentId().toString());
            apLikesBehavior.setArticleId(dto.getArticleId());
            apLikesBehavior.setCreatedTime(new Date());
            apLikesBehavior.setOperation(dto.getOperation());
            //保存到数据库
            mongoTemplate.save(apLikesBehavior);
        } else {
            //取消点赞操作
            Query query = Query.query(Criteria.where("entryId").is(dto.getEquipmentId().toString()).and("articleId").is(dto.getArticleId()));
            ApLikesBehavior apLikesBehavior = mongoTemplate.findOne(query, ApLikesBehavior.class);
            if (apLikesBehavior == null) {
                //还没有点赞
                CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST, "您还没有点赞,不能取消哦!");
            }
            mongoTemplate.remove(apLikesBehavior);
        }
        //创建NewBehaviorDTO,用于发送MQ消息
        NewBehaviorDTO newBehaviorDTO = new NewBehaviorDTO();
        newBehaviorDTO.setType(BehaviorType.LIKES);
        newBehaviorDTO.setArticleId(dto.getArticleId());
        newBehaviorDTO.setAdd(dto.getOperation() == 0 ? 1 : -1);
        //发送MQ消息
        rabbitTemplate.convertAndSend(HotArticleConstants.HOT_ARTICLE_SCORE_BEHAVIOR_QUEUE, JSON.toJSONString(newBehaviorDTO));
        //响应给web层
        return ResponseResult.okResult();
    }
}